home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / gopherd / Waisindex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-11  |  10.4 KB  |  422 lines

  1. /********************************************************************
  2.  * $Author: lindner $
  3.  * $Revision: 1.4 $
  4.  * $Date: 1993/01/05 02:41:28 $
  5.  * $Source: /home/mudhoney/GopherSrc/release1.11/gopherd/RCS/Waisindex.c,v $
  6.  * $State: Rel $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: Waisindex.c
  14.  * Routines to translate wais indexes on disk to gopher
  15.  *********************************************************************
  16.  * Revision History:
  17.  * $Log: Waisindex.c,v $
  18.  * Revision 1.4  1993/01/05  02:41:28  lindner
  19.  * .cap files are now ignored by the indexer
  20.  *
  21.  * Revision 1.3  1993/01/01  00:12:41  lindner
  22.  * Fixed parameters to GDnew()
  23.  *
  24.  * Revision 1.2  1992/12/21  20:36:44  lindner
  25.  * Added #include for cutil.h (from dgg)
  26.  *
  27.  * Revision 1.1  1992/12/10  23:13:27  lindner
  28.  * gopher 1.1 release
  29.  *
  30.  *
  31.  *********************************************************************/
  32.  
  33. #if defined(WAISSEARCH)
  34.  
  35. /* WIDE AREA INFORMATION SERVER SOFTWARE
  36.    No guarantees or restrictions.  See the readme file for the full standard
  37.    disclaimer.    
  38.    Brewster@think.com
  39.  
  40.    Heavily hacked by Paul Lindner (lindner@boombox.micro.umn.edu)
  41.    Do you even recognize this Brewster? :-)
  42.  
  43. */
  44.  
  45. int ShowDate = 0;
  46.  
  47. #define _search_c
  48.  
  49. #include "gopherd.h"
  50.  
  51.  
  52. #if defined(_AIX)
  53. #define ANSI_LIKE
  54. #endif
  55.  
  56. #include "../ir/irext.h"
  57. #include "../ir/irsearch.h"
  58. #include "../ir/docid.h"
  59. #include "../ir/irtfiles.h"
  60. #include "../ir/cutil.h"    /** fix for -DBIO wais needs.. **/
  61. #include <math.h>
  62.  
  63.  
  64. FILE *logfile = NULL; /* the logfile */
  65. char *log_file_name = NULL;
  66.  
  67. static char *DefaultDB = "index";
  68. static char *MonthStr[] = {
  69.      "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Sept", "Oct",
  70.       "Nov", "Dec"
  71. };
  72.  
  73. #if defined(void)
  74. #undef void
  75. #endif
  76.  
  77.  
  78.  
  79. int
  80. Process_Veronica(besthit, gs)
  81.   hit *besthit;
  82.   GopherObj *gs;
  83. {
  84.      FILE *ZeFile;
  85.      char veronicabuf[1024];
  86.      char *data, *cp;
  87.  
  88.      /*** Open up the file and seek to the right position ***/
  89.  
  90.      ZeFile = ufopen(besthit->filename, "r");
  91.  
  92.      if (ZeFile == NULL)
  93.       return(-1);
  94.  
  95.      fseek(ZeFile, besthit->start_character, 0);
  96.  
  97.      bzero(veronicabuf, sizeof(veronicabuf));
  98.      fread(veronicabuf, 1, besthit->end_character - besthit->start_character,
  99.        ZeFile);
  100.      veronicabuf[besthit->end_character - besthit->start_character+1] = '\0';
  101.      
  102.      
  103.      data = veronicabuf;
  104.      GSsetType(gs, *data);
  105.      
  106.      ZapCRLF(data);
  107.      
  108.      cp = strchr(data, '\t');
  109.      *cp = '\0';
  110.      GSsetTitle(gs, data+1);
  111.      
  112.      data = cp+1;
  113.      cp = strchr(data, '\t');
  114.      *cp = '\0';
  115.      GSsetPath(gs, data);
  116.      
  117.      data = cp + 1;
  118.      cp = strchr(data, '\t');
  119.      *cp = '\0';
  120.      GSsetHost(gs, data);
  121.      
  122.      GSsetPort(gs, atoi(cp+1));
  123.  
  124.      fclose(ZeFile);
  125.      return(0);
  126. }
  127.  
  128. void
  129. WaisIndexQuery(sockfd, index_directory, SearchWords, new_db_name, INDEXHost, INDEXPort, INDEXPath)
  130.   int sockfd;
  131.   char *index_directory;
  132.   char *SearchWords;
  133.   char *new_db_name;
  134.   char *INDEXHost;
  135.   int  INDEXPort;
  136.   char *INDEXPath;
  137.      database* db;
  138.      long maxRawScore;
  139.      long normalScore;
  140.      char *cp;
  141.      char *Selstrout;
  142.      char dateline[10];
  143.      long i;
  144.      query_parameter_type parameters;
  145.      boolean search_result;
  146.      char score[6];
  147.      static char ReturnLine[512];
  148.                                         
  149.      char * sidename;                    /* mtm 11-23-92 */
  150.      FILE * SideFile = NULL;             /* mtm 11-23-92 */
  151.  
  152.      GopherDirObj *gd;
  153.      GopherObj    *gs;
  154.      
  155.  
  156.      gs = GSnew();
  157.      gd = GDnew(32);
  158.  
  159.      if (DEBUG)  {
  160.       fprintf(stderr, "IndexPath: %s\n", INDEXPath);
  161.       logfile = stderr;   /** Log wais error messages to console **/
  162.      }     else {
  163.       logfile = ufopen("/dev/null", "w+");
  164.      }
  165.  
  166.      if (new_db_name == NULL) {
  167.       new_db_name = DefaultDB;
  168.      }
  169.  
  170.      if (uchdir(index_directory)) {
  171.       Abortoutput(sockfd, "Couldn't change to index directory...");
  172.       return;
  173.      }
  174.  
  175.      if (SearchWords != NULL && strlen(SearchWords) == 0) {
  176.       EveryWAISdocument(new_db_name);
  177.       return;
  178.      }
  179.  
  180.      db = openDatabase(new_db_name, false, true);
  181.      
  182.      if (db == NULL) {
  183.       sprintf(ReturnLine, "Failed to open database %s in index dir %s", new_db_name, index_directory);
  184.       Abortoutput(sockfd, ReturnLine);
  185.       writestring(sockfd, ".\r\n"); /** be polite **/
  186.       return;
  187.      }
  188.      
  189. #ifdef BIO            /* dgg */
  190. {
  191.      char *cp= read_delimiters( db);  /* use data-specific delim, available */
  192.  
  193.      if (cp != NULL) {
  194.       strcpy( gDelimiters, cp);
  195.       wordDelimiter= wordbreak_user;
  196.      }
  197.      else
  198.       wordDelimiter= wordbreak_notalnum;
  199. }
  200. #endif
  201.  
  202.      parameters.max_hit_retrieved = 256;
  203.  
  204.      set_query_parameter(SET_MAX_RETRIEVED_MASK, ¶meters);
  205.      
  206.      search_result = false;
  207.      search_result |= search_for_words(SearchWords, db, 0);
  208.      
  209.      if (search_result == true) {
  210.       /* the search went ok */
  211.       hit best_hit;
  212.       
  213.       finished_search_word(db);
  214.       if (DEBUG)
  215.            printf("After finished_search\n");
  216.  
  217.       uchdir(Data_Dir); /* necessary to find side files */
  218.  
  219.       for (i = 0; i < parameters.max_hit_retrieved; i++){ 
  220.            if (0 != next_best_hit(&best_hit, db))
  221.             break;        /* out of hits */
  222.            if (i == 0)
  223.             maxRawScore = best_hit.weight;
  224.            if (best_hit.weight > 0 && 
  225.            strstr(best_hit.filename, ".cache")==NULL &&
  226.            strstr(best_hit.filename, ".cap/")==NULL){
  227.             long lines,length;
  228.  
  229.             char** type = NULL;
  230.             
  231.             normalScore = (long)floor((((double)best_hit.weight) /
  232.                            ((double)maxRawScore)) *    
  233.                           (MAX_NORMAL_SCORE + 1));
  234.  
  235.             if (normalScore > MAX_NORMAL_SCORE)
  236.              normalScore = MAX_NORMAL_SCORE;
  237.             
  238.  
  239.             /*** Strip off the first part of the path in the filename*/
  240.             /*** Plus it gets rid of weird automount things... ***/
  241.             Selstrout =strstr(best_hit.filename, INDEXPath);
  242.             if (Selstrout == NULL)
  243.              Selstrout = "Error in Hostdata!";
  244.             else
  245.              Selstrout += strlen(INDEXPath);
  246.             
  247.  
  248.                     sprintf(score,"%3d ",best_hit.weight);
  249.  
  250.                     waislog(0,99,"%s: Score %3d:%s",SearchWords,best_hit.weight,Selstrout);
  251.             
  252.             /** Make the outgoing string **/
  253.  
  254.             ZapCRLF(best_hit.headline);
  255.             
  256.             /*** Remove the gopher data directory pathname if
  257.                  it's there from the headline
  258.             ***/
  259.  
  260.             if ((cp = strstr(best_hit.headline, INDEXPath)) != NULL) {
  261.              /*** Dangerous.... ***/
  262.              strcpy(cp, cp+strlen(INDEXPath));
  263.             } 
  264.              
  265.             GSsetType(gs, '0');
  266.             GSsetTitle(gs, best_hit.headline);
  267.             GSsetHost(gs, INDEXHost);
  268.             GSsetPort(gs, INDEXPort);
  269.  
  270.                  /* removed "/" from following line (before %s) . 
  271.                 Was getting double slash at least with w8b5bio; 
  272.                 mtm 11-23-92 */
  273.  
  274.             sprintf(ReturnLine, "R%d-%d-%s",
  275.                 best_hit.start_character, best_hit.end_character,
  276.                 Selstrout);
  277.             
  278.             if (!MacIndex)
  279.             GSsetPath(gs, ReturnLine);
  280.             else
  281.             GSsetPath(gs, Selstrout);
  282.             GSsetWeight(gs, best_hit.weight);
  283.             
  284.                     /* 
  285.              * Find and process sidefile. 
  286.              * Allow worst case name length. 
  287.              */
  288.  
  289.             if((sidename = (char *) malloc((unsigned) 
  290.                 strlen(Selstrout) + 
  291.                     strlen("/.cap/") + 1)) != NULL) {
  292.               if((cp = mtm_basename(Selstrout)) != Selstrout) {
  293.             /*  turn "/foo/bar/baz" into "/foo/bar/.cap/baz" */
  294.             strncpy(sidename,Selstrout,(cp - Selstrout));
  295.             *(sidename + (cp - Selstrout)) = '\0';
  296.             strcat(sidename,".cap/");
  297.             strcat(sidename,cp);
  298.               }
  299.               else {
  300.               /* root of the gopher tree, this is easier... */
  301.             strcpy(sidename,"/.cap/");
  302.             strcat(sidename,Selstrout);
  303.               }
  304.               if ((SideFile = rfopen(sidename, "r")) != NULL) {
  305.             if (DEBUG == TRUE)
  306.               printf("Side file name: %s\n", sidename);
  307.             Process_Side(SideFile, gs);
  308.               }
  309.               free(sidename);
  310.             }
  311.             
  312.             if (DEBUG) printf("Doc type is %s\n", best_hit.type);
  313.             if (strcmp(best_hit.type, "GOPHER")==0) {
  314.             if (DEBUG) printf("Got a veronica style thing %s\n",best_hit.headline);
  315.             Process_Veronica(&best_hit, gs);
  316.            }            
  317.             
  318.             GStoNet(gs,sockfd);
  319.  
  320.            }
  321.            
  322.            
  323.            if (DEBUG) {
  324.             printf("%s\n", ReturnLine);
  325.             printf("End Byte   = %d\n", best_hit.end_character);
  326.             printf("Doc length = %d\n", best_hit.document_length);
  327.             printf("#lines     = %d\n", best_hit.number_of_lines);
  328.            }
  329.       }
  330.      }
  331.      else {
  332.       /* something went awry in the search */
  333.       LOGGopher(sockfd, "Something went wrong in the search!\r\n");
  334.       writestring(sockfd, ".\r\n"); /*** be polite, don't screw up the client**/
  335.       return;
  336.      }
  337.      finished_best_hit(db);
  338.  
  339.      writestring(sockfd, ".\r\n");
  340.  
  341.      /* free everything */
  342.      closeDatabase(db);
  343.      return;
  344. }
  345.  
  346. EveryWAISdocument(sockfd, db, INDEXHost, INDEXPort, INDEXPath)
  347.   int sockfd;
  348.   char *db;
  349.   char *INDEXHost;
  350.   int  INDEXPort;
  351.   char *INDEXPath;
  352. {
  353.      FILE         *dbcatalog;
  354.      char         db_name[MAXPATHLEN];
  355.      char         inputline[512];
  356.      String       *Headline;
  357.      String       *Filename;
  358.      int          StartByte, EndByte;
  359.      GopherObj    *gs;
  360.      GopherDirObj *gd;
  361.      boolean      Headlineset = FALSE;
  362.      boolean      DocIDset    = FALSE;
  363.  
  364.      gs = GSnew();
  365.      gd = GDnew(32);
  366.      Headline = STRnew();
  367.      Filename = STRnew();
  368.  
  369.      strcpy(db_name, db);
  370.      strcat(db_name, ".cat");
  371.  
  372.      dbcatalog = rfopen(db_name, "r");
  373.      
  374.      while (fgets(inputline, sizeof(inputline), dbcatalog) != NULL) {
  375.       if (strncmp(inputline, "Headline: ", 10)==0) {
  376.            STRset(Headline, inputline +10);
  377.            Headlineset = TRUE;
  378.       }
  379.       else if (strncmp(inputline, "DocID: ", 7)==0) {
  380.            char *cp;
  381.  
  382.            StartByte = atoi(inputline);
  383.            cp = strchr(inputline+7, ' ');
  384.            if (cp == NULL) break;
  385.  
  386.            cp++;
  387.            EndByte = atoi(cp);
  388.  
  389.            cp = strchr(inputline+7, ' ');
  390.            cp++;
  391.            if (cp == NULL) break;
  392.  
  393.            cp =strstr(cp, INDEXPath);
  394.            if (cp == NULL) break;
  395.            
  396.            STRset(Filename, cp);
  397.  
  398.            DocIDset = TRUE;
  399.       }
  400.       
  401.       if (DocIDset == TRUE && Headlineset == TRUE) {
  402.            char tmppath[512];
  403.  
  404.            sprintf(tmppath, "R%d-%d-%s", StartByte, EndByte, STRget(Filename));
  405.  
  406.            GSsetType(gs, '0');
  407.            GSsetTitle(gs, STRget(Headline));
  408.            GSsetHost(gs, INDEXHost);
  409.            GSsetPort(gs, INDEXPort);
  410.            GSsetPath(gs, tmppath);
  411.  
  412.            GDaddGS(gd, gs);
  413.  
  414.            DocIDset = FALSE;
  415.            Headlineset = FALSE;
  416.       }
  417.      }      
  418. }
  419.  
  420. #endif /** WAISSEARCH **/
  421.